/*--------------------------------------------------------------------------------------*\
**
** t_adventure_event_base.h
**
** Heroes IV
** Copyright 2000, The 3DO Company
**
\*--------------------------------------------------------------------------------------*/

#if !defined( ADVENTURE_EVENT_BASE_H_INCLUDED)
#define ADVENTURE_EVENT_BASE_H_INCLUDED

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <list>

#include "counted_ptr.h"
#include "handler.h"
#include "memory_buffer.h"
#include "adventure_object_global_id.h"
#include "adventure_object_memory_cache.h"

enum {
	k_mover_event				= 1,
	k_place_event				= 2,
	k_remove_event				= 3,
	k_teleport_event			= 4,
	k_update_event				= 5,
	k_visiblity_event			= 6,
	k_set_action_event			= 7,
	k_set_owner_event			= 8,
	k_create_adv_spell_effects	= 9,
	k_look_trigger				= 10
};

class  t_adventure_map;
class  t_adventure_object;
struct t_saved_game_header;

class t_adventure_event_base;

typedef t_handler_1<t_adventure_event_base*>	t_adventure_event_handler;

namespace t_adventure_event_functions
{
	// --------------------------------------------------------------------------
	// k_memory_event_buffer_size : defines the desired working size of the in memory buffer
	// object. memory buffers keep a working buffer which is preallocated to avoid constantly
	// growing the vector.
	// --------------------------------------------------------------------------
	extern int k_memory_event_buffer_size ;

	// --------------------------------------------------------------------------
	// seralize the adv_object + header and reutrn the in memory buffer, compacted so as to save space
	// --------------------------------------------------------------------------
	t_memory_buffer_counted_ptr			create_event_buffer(t_adventure_object * adv_object);

	// --------------------------------------------------------------------------
	// seralize the adv_object and reutrn the in memory buffer, compacted so as to save space
	// --------------------------------------------------------------------------
	t_memory_buffer_counted_ptr			create_state_cahce_buffer(t_adventure_object * adv_object);

	// --------------------------------------------------------------------------
	// seralize the adv_object and reutrn the in memory buffer, compacted so as to save space
	// --------------------------------------------------------------------------
	void								read_object_from_state_cache (t_adventure_object * adv_object, 
												t_memory_buffer_counted_ptr m_event_buffer_state );

	// --------------------------------------------------------------------------
	// creates a adv_object using the maps adv_object creation function.
	// --------------------------------------------------------------------------
	t_adventure_object* 			create_and_place_adv_object( t_adventure_map* map, t_memory_buffer_counted_ptr adv_buffer, 
		t_saved_game_header const & header);
};

// --------------------------------------------------------------------------
// t_adventure_event_base class
// --------------------------------------------------------------------------

class t_adventure_event_base : public t_counted_object, public t_adventure_object_memory_cache_refrence
{
public:
	// Constructor
	t_adventure_event_base( int event_type );
	
	bool			attach_event_to_cache( t_adventure_map* map );

	virtual void	cancel_event();
	virtual void	execute_event( t_adventure_map* map , t_saved_game_header const & header );
	virtual bool	finished( );
	
	unsigned int	get_event_tick( );
	int				get_event_type( );
	int				get_team_turn_on( );
	
	virtual bool	is_valid();
	
	virtual bool	read( std::streambuf & buffer , int version );
	
	void			set_event_tick( unsigned int tick );
	void			set_handler( t_adventure_event_handler const& handler );
	void			set_players_turn_on( int player_turn_on );
	
	virtual bool	write( std::streambuf & buffer );
	
	virtual void	undo_event( t_adventure_map* map , t_saved_game_header const & header );
	virtual void	update( );
	
protected:

	t_adventure_event_handler		m_adv_event_handler;
	unsigned int					m_event_tick;
	int								m_event_type;
	t_adventure_global_id			m_global_id;

	bool							m_is_finished;
	bool							m_is_valid;

	int								m_player_turn_on;
	unsigned int					m_time_end;
	
};

inline unsigned int t_adventure_event_base::get_event_tick( )
{
	return m_event_tick;
}

inline 	void t_adventure_event_base::set_players_turn_on( int player_turn_on ) 
{
	m_player_turn_on = player_turn_on;
}

inline void	t_adventure_event_base::set_event_tick( unsigned int tick )
{
	m_event_tick = tick;
}

typedef t_counted_ptr<t_adventure_event_base>	t_adventure_event_ptr;
typedef std::list<t_adventure_event_ptr>		t_adventure_event_list;

#endif // ADVENTURE_EVENT_BASE_H_INCLUDED

